//
//  FMDBData.m
//  Homework
//
//  Created by xalo on 16/6/20.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "FMDBData.h"
#import <FMDB.h>
#import "Account.h"

@interface FMDBData ()

@property (nonatomic, strong)NSString *fileName;

@property (nonatomic, strong)FMDatabase *db;
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation FMDBData

//懒加载
- (NSString *)fileName{
    if (!_fileName) {
        _fileName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"FMDB.db"];
    }
    return _fileName;
}

- (FMDatabase *)db{
    if (!_db) {
        _db = [FMDatabase databaseWithPath:self.fileName];
    }
    return _db;
}

//单例
+ (instancetype)sharedFMDB {
    static FMDBData *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[FMDBData alloc] init];
    });
    return handle;
}

//@"CREATE TABLE IF NOT EXISTS STUDENT (name text,age text)"
//创建表
- (void)creatTableSqlString:(NSString *)string{
    //打开数据库
    if ([self.db open]) {
        //创建表
        [self.db executeUpdate:string];
    }
}

//@"INSERT INTO STUDENT(name,age) VALUES(?,?)",name,age
//增
- (void)insertDataWithSqlString:(NSString *)string{
    //打开数据库
    [self.db open];
    //插入
    [self.db executeUpdate:string];
    //关闭数据库
    [self.db close];
}

//@"DELETE FROM STUDENT WHERE (name = ?)",name
//删
- (void)deleteDataWithSqlString:(NSString *)string{
    [self.db open];
    [self.db executeUpdate:string];
    [self.db close];
}
//@"UPDATE STUDENT SET age = ?WHERE age = ?",newAge,oldAge
//改
- (void)updateDataWithSqlString:(NSString *)string{
    [self.db open];
    [self.db executeUpdate:string];
    [self.db close];
}
//@"SELECT *FROM STUDENT"
//查
- (NSString *)selectDataSqlString:(NSString *)string content:(NSString *)name{
    [self.db open];
    NSString *cont = [NSString string];
    FMResultSet *set = [self.db executeQuery:string];
    while ([set next]) {
        cont = [set objectForColumnName:name];
    }
    [self.db close];
    return cont;
}
- (void)insertDataWithSqlString:(NSString *)string string:(NSString *)string2{
    //打开数据库
    [self.db open];
    //插入
    BOOL isInsert = [self.db executeUpdate:@"INSERT INTO account(uname,password) VALUES(?,?)",string,string2];
    
    if (isInsert) {
        NSLog(@"chenggong");
    } else {
        NSLog(@"shibai");
    }
    //关闭数据库
    [self.db close];
}

//查
- (Account *)select{
    [self.db open];
    //查询整张表
    FMResultSet *set = [self.db executeQuery:@"SELECT *FROM account"];
    while ([set next]) {
        NSString *uname = [set objectForColumnName:@"uname"];
        NSString *password = [ set objectForColumnName:@"password"];
        Account  *acc = [[Account alloc]initWithName:uname password:password];
        NSLog(@"%@",acc);
        self.array=[NSMutableArray array];
        [self.array addObject:acc];
    }
    NSLog(@"%@",self.array[self.array.count-1]);
    Account *stu = self.array[self.array.count-1];
    NSLog(@"%@",stu.uname);
    
    [self.db close];
    
    return stu;
}
@end
